package com.leetcode.algorithm.y22.m06;

/**
 * 557. 反转字符串中的单词 III
 * 
 * https://leetcode.cn/problems/reverse-words-in-a-string-iii/
 * 
 * @author jie.deng
 *
 */
public class Question0557Solution01 {

	public String reverseWordsIII(String s) {
		StringBuilder builder = new StringBuilder(s.length());
		int len = s.length();
		int idx = 0;
		while (idx < len) {
			int left = idx;
			while (idx < len && s.charAt(idx) != ' ') {
				idx++;
			}
			int right = idx - 1;
			for (int i = right; i >= left; i--) {
				builder.append(s.charAt(i));
			}
			if (idx < len) {
				builder.append(' ');
				idx++;
			}
		}

		return builder.toString();
	}
}
